//
//  WJBaseViewController.swift
//  WJWeibo
//
//  Created by Jie on 15/12/16.
//  Copyright © 2015年 周文杰. All rights reserved.
//

import UIKit

class WJBaseViewController: UITableViewController {

    //用户是否登录
    var userLogin = false
    var vistorView : WJVistorView?
    
    override func viewDidLoad() {
        super.viewDidLoad()


    }
    override func loadView() {
        //判断是否登录
        userLogin ? super.loadView() : loadVistorView()
    }
    
    //加载访客视图
    func loadVistorView(){

        setupVistorView()

    }

    ///设置访客视图
    func setupVistorView() {
        vistorView = WJVistorView()
        view = vistorView
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorViewRegister")
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorViewLogin")
        //切换控制器时判断是哪个控制器来加载控件
        if self is WJHomeViewController {
            vistorView!.starResellAnim()
            //监听应用程序是否退回到桌面，退回则停止动画 回来则开始动画
            //回到桌面
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
            //回到应用程序
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)
            
        }else if self is WJMessageViewController {
            vistorView!.changeUI("visitordiscover_image_message", text: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
        }else if self is WJDiscoverViewController {
            vistorView!.changeUI("visitordiscover_image_message", text: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
        }else if self is WJProfileViewController {
            vistorView!.changeUI("visitordiscover_image_profile", text: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        }
        vistorView?.vistorViewDelegate = self
        
    }
    
    //MARK: - 暂停和开始动画
    ///返回桌面
    func didEnterBackground() {
        //暂停动画
        vistorView?.pauseResellAnim()
    }

    ///回到应用程序
    func didBecomeActive() {
        //开始动画
        vistorView?.resumeResellAnim()
    }

}

// MARK: - CZBaseTableViewController 扩展,实现 CZVistorViewDelegate 协议
extension WJBaseViewController: WJVistorViewDelegate {
    func vistorViewRegister() {

    }
    
    func vistorViewLogin() {

        presentViewController(UINavigationController(rootViewController: WJOAuthViewController()), animated: true) { () -> Void in
            
            
        }
        
    }
}
